package me.pplay.playtalk.activity.setting;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.pplay.playtalk.R;
import me.pplay.playtalk.activity.BaseActivity;
import me.pplay.playtalk.adapter.CommonAdapter;
import me.pplay.playtalk.bean.CommonControlID;
import me.pplay.playtalk.callback.CommonAdapterOnClickCallBack;
import me.pplay.playtalk.data.Globals;
import me.pplay.playtalk.database.IniDb;
import me.pplay.playtalk.protocol.bean.KeyValue;
import me.pplay.playtalk.util.LogManager;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class SettingAutoTextActivity extends BaseActivity {
	private List<Map<String, Object>> mData;
	private ListView list;
	List<Map<String, Object>> listdata = new ArrayList<Map<String, Object>>();
	private ImageView set_autotext_edit;
	private CommonAdapter adapter;
	IniDb inidb = new IniDb(Globals.FILE_PATH_CACHE_FILE + "autotext.ini");

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_setting_autotext);
		super.SetOnBackClickListener((ImageButton)findViewById(R.id.btn_back));
		mData = getData();
		CommonControlID holderId = new CommonControlID();
		holderId.itemLayout = R.layout.layout_setting_autotextlist;
		holderId.leftStrId = R.id.set_autotext_item_left;
		holderId.rightStrId = R.id.set_autotext_item_right;
		adapter = new CommonAdapter(SettingAutoTextActivity.this, mData, holderId, new CommonAdapterOnClickCallBack() {

			@Override
			public void OnClickCallback(int pos, int ViewId, View v) {
				// TODO Auto-generated method stub
				LogManager.LogShow("SettingAutoTextActivity", "OnClickCallback position " + pos);

			}
		});
		list = (ListView) this.findViewById(R.id.set_autotext_list); // 联系人ListView
		list.setAdapter(adapter);// 将数据适配器与Activity进行绑定
		set_autotext_edit = (ImageView) findViewById(R.id.set_autotext_edit);
		clickEvents();
	}

	private void clickEvents() {
		set_autotext_edit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				LayoutInflater inflater = getLayoutInflater();

				final  View layout = inflater.inflate(R.layout.layout_setting_autotext_add, (ViewGroup) findViewById(R.id.set_autotext_add_dialog));
				AlertDialog show = new AlertDialog.Builder(SettingAutoTextActivity.this).setTitle(getResources().getString(R.string.set_autotext)).setView(layout).setPositiveButton("Save", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						EditText editText1 = (EditText) layout.findViewById(R.id.set_autotext_addtype);
						EditText editText2 = (EditText) layout.findViewById(R.id.set_autotext_addreplace);
						String set_autotext_addtype = editText1.getText().toString().trim();
						String set_autotext_addreplace = editText2.getText().toString().trim();
						if (!set_autotext_addtype.equals("") && !set_autotext_addreplace.equals("")) {
							inidb.add(set_autotext_addtype, set_autotext_addreplace);
							inidb.commit();
							getData();
							dialog.dismiss();
							adapter.notifyDataSetChanged();
						} else {
							showToast(getString(R.string.string_isnull));
						}

					}
				}).show();
			}
		});
	}



	private List<Map<String, Object>> getData() {
		listdata.clear();
		ArrayList<KeyValue> dblist = inidb.getAll();
		for (KeyValue kv : dblist) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("leftStr", kv.key);
			map.put("rightStr", kv.value);
			listdata.add(map);
		}
		return listdata;
	}
}
